package com.zbcbms.controller;


import com.zbcbms.common.AlertUtilCommon;
import com.zbcbms.common.GetChartCommon;
import com.zbcbms.entity.tableData.UserTableData;
import com.zbcbms.service.UserService;
import com.zbcbms.service.impl.UserServiceImpl;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author fengq
 */
public class QueryUserController implements Initializable {

    @FXML
    private TableView<UserTableData> searchUserView;
    @FXML
    private TableColumn<UserTableData,String> searchUserCode;
    @FXML
    private TableColumn<UserTableData,String> searchUserName;
    @FXML
    private TableColumn<UserTableData,String> searchJoinTime;
    @FXML
    private TableColumn<UserTableData,String> searchRole;
    @FXML
    private TableColumn operation;
    @FXML
    private PieChart userRolePieChart;
    @FXML
    private TextField searchText;
    @FXML
    private StackedBarChart<String,Number> userJoinCountStackedBarChart;
    private GetChartCommon getChartCommon=new GetChartCommon();
    private UserService userService=new UserServiceImpl();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        userTableInit(searchUserView,searchUserCode,searchUserName,searchJoinTime,searchRole,operation,userService.getAllUserTableData());
        userRolePieChart.setData(getChartCommon.getUserRoleChartData());
        userJoinCountStackedBarChart.setData(getChartCommon.getUserJoinCountStackedBarChart());
    }

    /**
     * 表格数据初始化
     * @param searchUserView
     * @param searchUserCode
     * @param searchUserName
     * @param searchJoinTime
     * @param searchRole
     * @param operation
     * @param data
     */
    public void userTableInit(TableView<UserTableData> searchUserView,
                              TableColumn<UserTableData, String> searchUserCode,
                              TableColumn<UserTableData,String>  searchUserName,
                              TableColumn<UserTableData,String>  searchJoinTime,
                              TableColumn<UserTableData,String>  searchRole,
                              TableColumn operation,
                              ObservableList<UserTableData> data){
        searchUserCode.setCellValueFactory(cellData->cellData.getValue().getUserNumber());
        searchUserName.setCellValueFactory(cellData->cellData.getValue().getUserName());
        searchJoinTime.setCellValueFactory(cellData->cellData.getValue().getJoinDate());
        searchRole.setCellValueFactory(cellDate->cellDate.getValue().getRole());
        operation.setCellFactory((col)->{
            TableCell<UserTableData,String> cell=new TableCell<UserTableData,String>(){
                public HBox addButton=new HBox();
                Label delBtn=new Label("删除");
                Label editBtn=new Label("编辑");
                Label empty = new Label("   ");
                {
                    addButton.getStylesheets().add("view/css/user.css");
                    addButton.setAlignment(Pos.CENTER);
                    delBtn.getStyleClass().addAll("delBtn","btn");
                    editBtn.getStyleClass().addAll("editBtn","btn");
                    addButton.getChildren().addAll(editBtn,empty,delBtn);
                }

                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (!empty){
                        delBtn.setOnMouseClicked((m)->{
                            UserTableData selectedItem = getTableView().getSelectionModel().getSelectedItem();
                            if (new AlertUtilCommon().AlterMessageOption(delBtn.getScene().getWindow(),"请你选择",
                                    "确认删除","取消删除","删除提示","是否删除该用户？")){
                                new UserServiceImpl().deleteById(selectedItem.getUserNumber().get());
                                initialize(null,null);
                            }
                        });
                        editBtn.setOnMouseClicked((m)->{
                            UserTableData selectedItem=getTableView().getSelectionModel().getSelectedItem();
                            AnchorPane anchorPane = (AnchorPane) getTableView().getParent().getParent().getParent();
                            HomePageController homePageController = new HomePageController();
                            homePageController.setMainFrameAnchorPane(anchorPane);
                            homePageController.do_toUpdateUser(selectedItem);
                        });
                        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                        setGraphic(addButton);
                    }else {
                        setGraphic(null);
                    }
                }
            };
            return cell;
        });
        searchUserView.setItems(data);
    }

    @FXML
    public void searchBtnAction(){
        String text = searchText.getText();
        ObservableList<UserTableData> allUserVoByKeyword = new UserServiceImpl().getAllUserVoByKeyword(text);
        userTableInit(searchUserView,searchUserCode,searchUserName,searchJoinTime,searchRole,operation,allUserVoByKeyword);
    }
}
